<?php
$code       = str_replace('.htm','',$html->getUrl(2));
switch($act)
{
    case "regnewletter":
        regnewletter();
        break;    
    case "subscribe":
        subscribe();
        break;
    case "unsubscribe":
        unsubscribe();
        break;
    case "inserttour":
        TImport('tour');
        InsertBooking();
        break;
    case "bookhotel":
        TImport('hotel');
        InsertBookingHotel();
        break;
    case "insertreview":
        TImport('review');
        InsertReview();
        break;
    case 'addcontactus':
        TImport('contactus');
        InsertContactUs();
        break;
    case 'addcart':
        TImport('product');
        AddCart();
        break;
    case 'updatecart':
        TImport('product');
        UpdateCart();
        break;
    case 'addorder':
        TImport('product');
        AddOrder();
        break;
    case 'addreview':
        TImport('review');
        InsertReview();
        break;
}

function regnewletter()
{
    global $dbsql,$root_domain,$titlesite;
    $email      = gpc_getStringPost("email");
    ImportClass('account');
    if(isValidEmail($email)==false)
    {
        AjaxTransfer('config',getLang('Please enter a valid email address').'!');
    }
    
    //check email exists:
    $sql_select = "select cEmail from tblnewsletter_email where cEmail='$email'";
    $rs         = $dbsql->query($sql_select);
    if($dbsql->num_rows($rs)==0)
	{
		$activatecode     = gpc_Md5(time().$email).time();
        $datepost         = date("Y-m-d");

		$sql_insert = "INSERT INTO tblnewsletter_email
            		(cEmail,cName,iStatus,iMonth,dRegDate)
            		 VALUES
            		('".$email."','".$activatecode."',0,0,'$datepost')";

		$dbsql->query($sql_insert);

		$linksubscribe     = echoLink("as/subscribe/".$activatecode.".htm",false,2);
		$linkunsubscribe   = echoLink("as/unsubscribe/".$activatecode.".htm",false,2);
        
        $linksubscribe      = '<a href="'.$linksubscribe.'">'.$linksubscribe.'</a>';
        $linkunsubscribe    = '<a href="'.$linkunsubscribe.'">'.$linkunsubscribe.'</a>';
        
        $emailconfig       = @getConfigEmail('RegisterNewletter');
    
        if($emailconfig==false)
        {
            AjaxTransfer('config','Email not config');
        }
        
        
        $subject          = $emailconfig->title;
        $subject          = str_ireplace('{SITE}',$titlesite,$subject);
        
        $message          = str_ireplace('{EMAIL}',$email,$emailconfig->content);
        $message          = str_ireplace('{SITE}',$titlesite,$message);
        $message          = str_ireplace('{LINKSUBSCRIBE}',$linksubscribe,$message);
        $message          = str_ireplace('{LINKUNSUBSCRIBE}',$linkunsubscribe,$message);
        
		send_email($email,"",$subject,$message);

		AjaxTransfer('ok',getLang("Thank you for signing up to our newsletter").'!');
	}
	else
	{
        AjaxTransfer('f',getLang("Email already exists").'!');
	}    
}

function subscribe()
{
    global $dbsql,$language,$code;

    $sql_select="select cEmail from tblnewsletter_email where cName='$code'";
    $rs=$dbsql->query($sql_select);
    if($result=$dbsql->fetch_array($rs))
    {
    	$sql_update = "update tblnewsletter_email set iStatus='1' where cEmail='".$result['cEmail']."'";
    	$dbsql->query($sql_update);
    	returnMsg('Your email Activated');
    }else
    {
    	returnMsg('Your email not exists in we system');
    }
}

function unsubscribe()
{
    global $dbsql,$language,$code;
       
    $sql_select="select cEmail from tblnewsletter_email where cName='$code'";
    $rs=$dbsql->query($sql_select);
    if($result=$dbsql->fetch_array($rs))
    {
    	//$sql_update = "update tblnewsletter set cStatus='NO' where cEmail='".$result['cEmail']."'";
    	//$dbsql->query($sql_update);
    	
    	$sql_delete = "delete from tblnewsletter_email where cEmail='".$result['cEmail']."'";
    	$dbsql->query($sql_delete);
    	
    	returnMsg('Your email Deleted');
    }else
    {
    	returnMsg('Your email Deleted');
    }
}

function returnMsg($msg)
{
    $html   = '<html><head><title>'.$msg.'</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    $html  .= '<meta http-equiv="REFRESH" content="10; url=/">';
    $msg   .= '<br><a href="/">'.getLang('go to home page').'</a>';
    $html  .= '<style>.msg{width:60%;padding:25px;text-align:center;color:#ff0000;border: 1px solid #ff0000}</style>';
    $html  .= '</head><body><div align=center><div class="msg">'.$msg.'</div></div></body></html>';
    exit($html);
}
?>